define(function(require, exports){
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var $ = require('jquery');
	var util = require('util');
    var popwin	= require('@base/dialog');

    var CERTIFICATE_FILE_TYPE = {TEXT:1,IMAGE:2,ZIP:3};
    //查看资质 弹窗
    var PopAptitude = popwin.base.extend({
        init: function(config){
            config = pubjs.conf(config, {
                'title': LANG('公司资质'),
                'size': 'm',
                // 显示底部
                "showFoot": false,
                'style': {
                    "layoutClass":"P-advertiserListInfoPopAptitude",
                },
                'vModel':{
                    'materials': []
                },
                'vMethods':{
                    'toUrl': this.toUrl,
                },
                'defaultImage': window.ROOT('resources/images/default.png')
            });
            this.Super('init', arguments);
        },
        afterBuild:function(){
            var self = this;
            self.Super('afterBuild', arguments);
            var el = self.getDOM();
            var c = self.getConfig();

            $([
                '<table class="table table-bordered mt20 mb20">'+
                    '<thead style="border:0">'+
                        '<tr>'+
                            '<th class="w25pct ">'+LANG('证件类型')+'</th>'+
                            '<th class="w25pct ">'+LANG('补充资质信息')+' </th>'+
                            '<th class="w25pct ">'+LANG('证件')+'</th>'+
                            '<th class="w25pct ">'+LANG('上传时间')+' </th>'+
                        '</tr>'+
                    '</thead>'+
                    '<tbody style="border:1px solid #cccccc" v-if="materials.length">'+
                        '<tr v-for="item in materials">'+
                            '<td class="w25pct "> {{item.Name}} </td>'+
                            '<td class="w25pct " v-if="item.CrtId">'+
                                '<div>文件ID: {{item.CrtId}} </div>'+
                                '<div>开始时间: {{item.CrtBeginDate}} </div>'+
                                '<div>结束时间: {{item.CrtEndDate}} </div>'+
                            '</td>'+
                            '<td class="w25pct " v-else> - </td>'+
                            '<td class="w25pct ">'+
                                '<div class="aptitudeImg" v-if="item.type==2">'+
                                    '<img alt="'+LANG('缩略图')+'" v-on:click="toUrl()" title="'+LANG('点击查看大图')+'"  v-bind:src="item.url"/>'+
                                '</div>'+
                                '<div v-if="item.type==1">'+
                                    '<a alt="'+LANG('地址')+'"  target="_blank" v-bind:href="item.url">跳转地址</a>'+
                                '</div>'+
                                '<div v-if="item.type==3">'+
                                     '<a alt="'+LANG('文件zip')+'" target="_blank" v-bind:href="item.url">文件zip</a>'+
                                '</div>'+
                            '</td>'+
                            '<td class="w25pct "> - </td>'+
                        '</tr>'+
                    '</tbody>'+
                     '<tbody style="border:1px solid #cccccc" v-else>'+
                         '<tr >'+
                            '<td class="w25pct "> - </td>'+
                            '<td class="w25pct " > - </td>'+
                            '<td class="w25pct "> - </td>'+
                            '<td class="w25pct "> - </td>'+
                        '</tr>'+
                    '</tbody>'+
                '</table>'
            ].join('')).appendTo(el);
            self.buildFromTemplate({
            });
            self.vm.mount();

        },
        //跳转
        toUrl: function(e){
            //编辑广告主
            if(e.path[0]="img"){
                window.open(e.path[0].currentSrc);
            }
        },
        reset: function(){
            var self = this;
            self.vm.reset();
            return self;
        },
        setData: function(data){
            var c = this.getConfig();
            var self = this;
            var certificateList = pubjs.config('certificateList');
            util.each(data,function(item,idx){
                util.each(certificateList,function(item2,idx2){
                    if(item2._id==item.TypeId){
                        //找到证件类型
                        item.type = CERTIFICATE_FILE_TYPE[util.clone(item2.Type)];
                        if(item.type==1 && item.Tanxurl){
                            item.url = item.Tanxurl;
                        }else if(item.type==2 && item.Thumb){
                            item.url = location.origin + item.Thumb +"&w=65&h=65";
                        }else if(item.type==3 && item.Thumb){
                            item.url = location.origin + item.Thumb +"&w=65&h=65";
                        }else{
                            item.url = window.ROOT('resources/images/default.png');
                        }
                        return;
                    }
                })
            })
            this.vm.$.materials = data;

            return this;
        }
    });
    exports.popAptitude = PopAptitude;

    //查看审核
    var PopStatus = popwin.base.extend({
        init: function(config){
            config = pubjs.conf(config, {
                'title': LANG('渠道审核'),
                'size': 'm',
                // 显示底部
                "showFoot": false,
                'style': {
                    "layoutClass":"P-advertiserListInfoPopStatus",
                },
                'vModel':{
                    'info':[],
                    'rootInfo':{ all:[],pass:[],review:[],reject:[] }
                },
                'vMethods':{
                },
            });
            this.Super('init', arguments);
        },
        afterBuild:function(){
            var self = this;
            self.Super('afterBuild', arguments);
            var el = self.getDOM();
            var c = self.getConfig();
            $([
                '<div class="mt20">'+
                    '<ul class="tab mb15 statusTab">'+
                        '<li data-tab="all" class="active">'+
                            '<a>'+LANG('全部')+'({{rootInfo.all.length}})</a>'+
                        '</li>'+
                        '<li data-tab="pass" class="">'+
                            '<a>'+LANG('通过')+'({{rootInfo.pass.length}})</a>'+
                        '</li>'+
                        '<li data-tab="review" class="">'+
                            '<a>'+LANG('审核中')+'({{rootInfo.review.length}})</a>'+
                        '</li>'+
                        '<li data-tab="reject" class="">'+
                            '<a>'+LANG('拒绝')+'({{rootInfo.reject.length}})</a>'+
                        '</li>'+
                    '</ul>'+
                'div'
            ].join('')).appendTo(el);
            $([
                '<table class="table table-bordered mt20 mb20" >'+
                    '<thead style="border:0;">'+
                        '<tr>'+
                            '<th class="w25pct tl">'+LANG('渠道')+'</th>'+
                            '<th class="w25pct tl">'+LANG('审核状况')+'</th>'+
                        '</tr>'+
                    '</thead>'+
                    '<tbody style="border:1px solid #cccccc">'+
                        '<tr v-for="item in info">'+
                            '<td class="w70pct tl"> {{item.Name}} </td>'+
                            '<td class="w30pct tl" v-if="item.Status==0"> <span class="statusIcon uk-icon-clock-o"></span><span class="statusText">'+LANG('审核中')+'</span></td>'+
                            '<td class="w30pct tl" v-if="item.Status==1"> <span class="statusIcon icon-checkbox-in font-success"></span><span class="statusText">'+LANG('通过')+'</span></td>'+
                            '<td class="w30pct tl" v-if="item.Status==-1"> <span class="statusIcon icon-close font-danger"></span><span class="statusText">'+LANG('拒绝')+'</td>'+
                        '</tr>'+
                    '</tbody>'+
                '</table>'
            ].join('')).appendTo(el);

            self.uiProxy(el.find(".statusTab"), 'li', 'click', 'eventSelected');
            // self.buildFromTemplate({
            // });
            self.vm.mount();
        },
        //选项卡点击事件
        eventSelected: function(evt, elm){
            if($(elm).hasClass('active')){
                return;
            }

            var rootInfo = this.vm.$.rootInfo;
            $(elm).addClass('active').siblings().removeClass('active');
            var tab = $(elm).data('tab');
            this.vm.$.info = rootInfo[tab];
        },
        reset: function(){
            var self = this;
            self.vm.reset();
            return self;
        },
        setData: function(data){
            this.reset();

            var self = this;
            var channelMap = pubjs.config('channelMap');
            var rootInfo = { all:[],pass:[],review:[],reject:[] };

            util.each(data, function(item){
                if(item){
                    var obj = {
                        Name: channelMap[item.ExchangeId],
                        Status: item.Status
                    }
                    rootInfo.all.push(obj);

                    switch(item.Status){
                        case 1 :
                            rootInfo.pass.push(obj);break;
                        case 0 :
                            rootInfo.review.push(obj);break;
                        case -1 :
                            rootInfo.reject.push(obj);break;
                    };
                }
            });

            self.vm.$.rootInfo = rootInfo;
            var active = self.getDOM().find(".statusTab .active");
            self.vm.$.info = rootInfo[active.data("tab")];

            return self;
        }
    });
    exports.popStatus = PopStatus;

    //选择广告主
	var SelectAdverPopwin = popwin.base.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'title': LANG('选择广告主'),
				'width': 900,
				'height': 600,
				'showSub': true,
				'class': 'P-messageManageEditPopwin',
				'noFilter': false,
				// 配置getData获取的数据是否为ids
				'selectIds': false,
				'view_model': {
				},
				'size': 'l',
			});
			this.$data = [];
			this.$selected = [];
			this.$ids = [];
			this.$result ={
				ids:[],
				data:[]
			};
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			self.Super('afterBuild',arguments);
			var el = self.getContainer('body');
			var c = this.getConfig();
			el.append($([
				'<div pub-name="grid" pub-mod="advertiser/grid/advertiser.selectAdver"></div>'
			].join('')));

			var config = {
				'grid': util.extend(this.getConfig('grid') || {}, {"param": {"Depth": 1}})
			};
			self.buildFromTemplate(config);
		},
		setParam: function (param) {
			this.$.grid.setParam(param);
			return this;
		},
		reload: function()
		{
			this.$.grid.reload();
		},
		onOk: function(){
			this.hide();
			this.fire('selectAdver', this.getData());
			return false;
		},
		onChangeSelect: function (ev) {
			//点击获取当前页相匹配的
			//bug: 当选择的换了分页无法得到前一页选择的东西，导致这个是因为遍历只有这一页的dada的时候
			//bug: highgrid 这个事件有三次
			var result = {
				ids:[],
				data:[]
			};
			this.$ids  = ev.param.selected;
			var self = this;
			var cData = this.changeData(this.$.grid.getData());
			//每次选中都缓存下来
			util.each(this.$ids, function(item){
				var id = item.hasOwnProperty('_id') ? item._id: item;
				if(cData[id]){
					self.$data[id] = cData[id];
				}
				//为了分页能读到Name,使用缓存$data
				if(self.$data[id]){
					result.data.push(self.$data[id]);
				}
			});
			result.ids = this.$ids;
			this.$result = result;
			return false;
		},
		setData: function(result, data) {
			if(result){
				this.$.grid.setSelectRowIds(result);
				this.$ids = result;
			}
			if(data)
			{
				for(var i =0 ; i<data.length;i++){
					var id = data[i].id;
					this.$data[id] = data[i];
				}
			}
			return this;
		},
		getData: function(){
			var ids = this.$ids;
			var c = this.getConfig();
			if (c.selectIds)
			{
				return ids;
			}
			else
			{
				this.onChangeSelect({
					param:{
						selected:ids
					}
				});
				return this.$result;
			}
		},
		changeData: function (data) {
			var result =[];
			util.each(data, function(item, idx){
				result[item._id] = {
					'id': item._id,
					'name': item.UserName
				};
			});
			return result;
		},
		reset: function(){
			// this.$data = {};
			this.$selected = [];
			this.$ids = [];
			this.$result = {
				ids:[],
				data:[]
			};
			// 设置好grid
			var grid = this.$.grid;
			grid.setSelectRowIds([]);
			// 重新加载数据
			grid.reload();
			return this;
		}
	});
	exports.selectAdverPopwin = SelectAdverPopwin;
});